# encoding: utf-8
from __future__ import absolute_import, unicode_literals

from avatars.sprite.base import SpriteBase


colors = {
  "bg": (
    ("#1e88e5", "#1976d2"),
    ("#039be5", "#0288d1"),
    ("#00acc1", "#0097a7"),
    ("#c0ca33", "#afb42b"),
    ("#fdd835", "#fbc02d"),
    ("#ffb300", "#ffa000"),
    ("#fb8c00", "#f57c00"),
    ("#f4511e", "#e64a19")
  ),
  "fg": (
    (
      ("#e57373", "#ef5350"),
      ("#f06292", "#ec407a"),
      ("#dce775", "#d4e157"),
      ("#fff176", "#ffee58"),
      ("#ffd54f", "#ffca28"),
      ("#ffb74d", "#ffa726"),
      ("#ff8a65", "#ff7043"),
      ("#eeeeee", "#e0e0e0")
    ),
    (
      ("#e57373", "#ef5350"),
      ("#f06292", "#ec407a"),
      ("#dce775", "#d4e157"),
      ("#fff176", "#ffee58"),
      ("#ffd54f", "#ffca28"),
      ("#ffb74d", "#ffa726"),
      ("#ff8a65", "#ff7043"),
      ("#eeeeee", "#e0e0e0")
    ),
    (
      ("#e57373", "#ef5350"),
      ("#f06292", "#ec407a"),
      ("#dce775", "#d4e157"),
      ("#fff176", "#ffee58"),
      ("#ffd54f", "#ffca28"),
      ("#ffb74d", "#ffa726"),
      ("#ff8a65", "#ff7043"),
      ("#eeeeee", "#e0e0e0")
    ),
    (
      ("#ba68c8", "#ab47bc"),
      ("#9575cd", "#7e57c2"),
      ("#7986cb", "#5c6bc0"),
      ("#64b5f6", "#42a5f5"),
      ("#4fc3f7", "#29b6f6"),
      ("#4dd0e1", "#26c6da"),
      ("#4db6ac", "#26a69a"),
      ("#eeeeee", "#e0e0e0")
    ),
    (
      ("#ba68c8", "#ab47bc"),
      ("#9575cd", "#7e57c2"),
      ("#7986cb", "#5c6bc0"),
      ("#64b5f6", "#42a5f5"),
      ("#4fc3f7", "#29b6f6"),
      ("#4dd0e1", "#26c6da"),
      ("#4db6ac", "#26a69a"),
      ("#616161", "#424242")
    ),
    (
      ("#ba68c8", "#ab47bc"),
      ("#9575cd", "#7e57c2"),
      ("#7986cb", "#5c6bc0"),
      ("#64b5f6", "#42a5f5"),
      ("#4fc3f7", "#29b6f6"),
      ("#4dd0e1", "#26c6da"),
      ("#4db6ac", "#26a69a"),
      ("#eeeeee", "#e0e0e0")
    ),
    (
      ("#ba68c8", "#ab47bc"),
      ("#9575cd", "#7e57c2"),
      ("#7986cb", "#5c6bc0"),
      ("#64b5f6", "#42a5f5"),
      ("#4fc3f7", "#29b6f6"),
      ("#4dd0e1", "#26c6da"),
      ("#4db6ac", "#26a69a"),
      ("#eeeeee", "#e0e0e0")
    ),
    (
      ("#9575cd", "#7e57c2"),
      ("#7986cb", "#5c6bc0"),
      ("#64b5f6", "#42a5f5"),
      ("#4fc3f7", "#29b6f6"),
      ("#4dd0e1", "#26c6da"),
      ("#4db6ac", "#26a69a"),
      ("#81c784", "#66bb6a"),
      ("#eeeeee", "#e0e0e0")
    )
  )
}
paths = {
    'body': (
        '<circle cx="12" cy="12" r="10"></circle>',
        '<path d="M 5,1 V 5 H 1 v 14 h 4 v 4 h 14 v -4 h 4 V 5 H 19 V 1 Z"></path>',
        '<path d="M 6,4.1450726 3,21 12,23 21,21 18,4.1450726 c -1.040085,-4.35794012 -11.004045,-4.17303435 -12,0 z"></path><path d="M 3.5,19 C 4.33,19 5,18.33 5,17.5 5,16.67 4.33,16 3.5,16 2.67,16 2,16.67 2,17.5 2,18.33 2.67,19 3.5,19 Z"></path><path d="M 4.5,15 C 5.33,15 6,14.33 6,13.5 6,12.67 5.33,12 4.5,12 3.67,12 3,12.67 3,13.5 3,14.33 3.67,15 4.5,15 Z"></path><path d="M 20.5,19 C 19.67,19 19,18.33 19,17.5 19,16.67 19.67,16 20.5,16 c 0.83,0 1.5,0.67 1.5,1.5 0,0.83 -0.67,1.5 -1.5,1.5 z"></path><path d="M 19.5,15 C 18.67,15 18,14.33 18,13.5 18,12.67 18.67,12 19.5,12 c 0.83,0 1.5,0.67 1.5,1.5 0,0.83 -0.67,1.5 -1.5,1.5 z"></path>',
        '<path d="M 14.444444,0.99999994 12,3.4444444 9.5555556,0.99999994 0.99999994,9.5555556 3.4444444,12 0.99999994,14.444444 9.5555556,23 12,20.555556 14.444444,23 23,14.444444 20.555556,12 23,9.5555556 Z"></path>',
        '<path d="M 4,2 5.5976562,9.9882812 1.0507812,14.535156 2.4648438,15.949219 6.0683594,12.345703 8,22 h 8 l 1.931641,-9.654297 3.603515,3.603516 1.414063,-1.414063 L 18.402344,9.9882812 20,2 Z"></path>',
        '<path d="M 21.999999,17.773502 12,23.547005 2.0000006,17.773502 l 0,-11.5470044 L 12,0.4529953 21.999999,6.2264977 Z"></path>',
        '<circle cy="10" cx="12" r="9"></circle><rect width="10" height="9" x="7" y="14" rx="1.6666667" ry="1"></rect>',
        '<path d="M 7,2 6,6 H 2 l 2,8 -2,8 H 22 L 20,14 22,6 H 18 L 17,2 Z"></path>'
    ),
    'eyes': (
        '<path d="M 15.5,11 C 16.33,11 17,10.33 17,9.5 17,8.67 16.33,8 15.5,8 14.67,8 14,8.67 14,9.5 c 0,0.83 0.67,1.5 1.5,1.5 z"></path><path d="M 8.5,11 C 9.33,11 10,10.33 10,9.5 10,8.67 9.33,8 8.5,8 7.67,8 7,8.67 7,9.5 7,10.33 7.67,11 8.5,11 Z"></path><path d="M 15.5,11 C 16.33,11 17,10.33 17,9.5 17,8.67 16.33,8 15.5,8 14.67,8 14,8.67 14,9.5 c 0,0.83 0.67,1.5 1.5,1.5 z"></path>',
        '<rect width="6" height="1" x="9.7279215" y="0.20710692" transform="rotate(45)"></rect><rect transform="rotate(135)" y="-13.227921" x="-2.2928932" height="1" width="6"></rect><rect transform="rotate(45)" y="-4.7426405" x="14.67767" height="1" width="6"></rect><rect width="6" height="1" x="-7.2426405" y="-18.17767" transform="rotate(135)"></rect>',
        '<path d="M 15.5,7 C 14.116667,7 13,8.1166667 13,9.5 13,10.883333 14.116667,12 15.5,12 16.883333,12 18,10.883333 18,9.5 18,8.1166667 16.883333,7 15.5,7 Z m 0,1 C 16.33,8 17,8.67 17,9.5 17,10.33 16.33,11 15.5,11 14.67,11 14,10.33 14,9.5 14,8.67 14.67,8 15.5,8 Z"></path><path d="M 8.5,7 C 7.116667,7 6,8.1166667 6,9.5 6,10.883333 7.116667,12 8.5,12 9.883333,12 11,10.883333 11,9.5 11,8.1166667 9.883333,7 8.5,7 Z m 0,1 C 9.33,8 10,8.67 10,9.5 10,10.33 9.33,11 8.5,11 7.67,11 7,10.33 7,9.5 7,8.67 7.67,8 8.5,8 Z"></path><path d="m 12,8 c -0.989493,0 -1.8112,0.857662 -2,2 h 0.753315 C 10.935894,9.418302 11.466399,9 12,9 c 0.533601,0 1.064106,0.418302 1.246685,1 H 14 C 13.8112,8.857662 12.989493,8 12,8 Z"></path>',
        '<path d="M 12,6.5 A 7,7 0 0 0 6.2617188,9.494141 7,7 0 0 0 12,12.5 7,7 0 0 0 17.738281,9.505859 7,7 0 0 0 12,6.5 Z M 12,7 c 1.383333,0 2.5,1.116667 2.5,2.5 C 14.5,10.883333 13.383333,12 12,12 10.616667,12 9.5,10.883333 9.5,9.5 9.5,8.116667 10.616667,7 12,7 Z m 0,1 c -0.83,0 -1.5,0.67 -1.5,1.5 0,0.83 0.67,1.5 1.5,1.5 0.83,0 1.5,-0.67 1.5,-1.5 C 13.5,8.67 12.83,8 12,8 Z"></path>',
        '<path d="m 12,8 c -0.989493,0 -1.8112,0.857662 -2,2 h 0.753315 C 10.935894,9.418302 11.466399,9 12,9 c 0.533601,0 1.064106,0.418302 1.246685,1 H 14 C 13.8112,8.857662 12.989493,8 12,8 Z"></path><rect width="6" height="6" x="5" y="7" rx="1" ry="1"></rect><rect ry="1" rx="1" y="7" x="13" height="6" width="6"></rect>',
        '<path d="m 15.888229,10.948889 c 1.603436,-0.42964 2.724368,-1.4236277 2.509548,-2.2253461 -0.214819,-0.8017185 -1.682569,-2.0581593 -3.286006,-1.6285196 -1.603436,0.4296396 -2.724368,2.3797154 -2.509548,3.1814337 0.214819,0.801719 1.682569,1.102071 3.286006,0.672432 z"></path><path d="M 8.1117707,10.948889 C 6.5083347,10.519249 5.3874027,9.5252613 5.6022227,8.7235429 5.8170417,7.9218244 7.2847917,6.6653836 8.8882287,7.0950233 10.491665,7.5246629 11.612597,9.4747387 11.397777,10.276457 11.182958,11.078176 9.7152077,11.378528 8.1117707,10.948889 Z"></path>',
        '<path d="m 6,7 -2,2 4,4 3,-1 V 10 L 10.95313,9.970703 C 10.733059,11.127302 9.7218293,12 8.5,12 7.116667,12 6,10.883333 6,9.5 6,8.7128681 6.3690487,8.0203566 6.9355469,7.5625 Z M 7.8867188,8.1328125 C 7.3644757,8.3671413 7,8.8893367 7,9.5 7,10.33 7.67,11 8.5,11 9.33,11 10,10.33 10,9.5 10,9.463163 9.99088,9.42874 9.988281,9.3925781 Z"></path><path d="M 18,7 17.064453,7.5625 C 17.630951,8.0203566 18,8.7128681 18,9.5 18,10.883333 16.883333,12 15.5,12 14.278171,12 13.266941,11.127302 13.046875,9.9707031 L 13,10 v 2 l 3,1 4,-4 z M 16.113281,8.1328125 14.011719,9.3925781 C 14.009124,9.4287398 14,9.4631634 14,9.5 14,10.33 14.67,11 15.5,11 16.33,11 17,10.33 17,9.5 17,8.8893367 16.635524,8.3671413 16.113281,8.1328125 Z"></path>',
        '<rect width="14" height="3" x="5" y="8" rx="1" ry="1"></rect>'
    ),
    'mouth': (
        '<path d="m 12,17.5 c 2.33,0 4.31,-1.46 5.11,-3.5 H 6.89 c 0.8,2.04 2.78,3.5 5.11,3.5 z"></path>',
        '<path d="m 12,14 c 2.33,0 4.31,1.46 5.11,3.5 H 6.89 C 7.69,15.46 9.67,14 12,14 Z"></path>',
        '<path d="m 12,17 c 0.83,0 1.5,-0.67 1.5,-1.5 0,-0.83 -0.67,-1.5 -1.5,-1.5 -0.83,0 -1.5,0.67 -1.5,1.5 0,0.83 0.67,1.5 1.5,1.5 z"></path>',
        '<path d="m 12,17.5 c 2.33,0 4.31,-1.46 5.11,-3.5 H 6.89 c 0.8,2.04 2.78,3.5 5.11,3.5 z"></path><path d="m 8,15 1,5 1,-5 z"></path><path d="m 14,15 1,5 1,-5 z"></path>',
        '<path d="m 6,14 c -0.554,0 -1,0.446 -1,1 v 3 c 0,0.554 0.446,1 1,1 h 12 c 0.554,0 1,-0.446 1,-1 v -3 c 0,-0.554 -0.446,-1 -1,-1 z m 1.5,1 C 7.777,15 8,15.223 8,15.5 v 2 C 8,17.777 7.777,18 7.5,18 7.223,18 7,17.777 7,17.5 v -2 C 7,15.223 7.223,15 7.5,15 Z m 3,0 c 0.277,0 0.5,0.223 0.5,0.5 v 2 C 11,17.777 10.777,18 10.5,18 10.223,18 10,17.777 10,17.5 v -2 C 10,15.223 10.223,15 10.5,15 Z m 3,0 c 0.277,0 0.5,0.223 0.5,0.5 v 2 C 14,17.777 13.777,18 13.5,18 13.223,18 13,17.777 13,17.5 v -2 C 13,15.223 13.223,15 13.5,15 Z m 3,0 c 0.277,0 0.5,0.223 0.5,0.5 v 2 C 17,17.777 16.777,18 16.5,18 16.223,18 16,17.777 16,17.5 v -2 C 16,15.223 16.223,15 16.5,15 Z"></path>',
        '<path d="m 8,14 c -0.554,0 -1,0.446 -1,1 v 2 c 0,0.554 1.446,2 2,2 h 6 c 0.554,0 2,-1.446 2,-2 v -2 c 0,-0.554 -0.446,-1 -1,-1 z m 1,1 h 2 v 2 c 0,0.554 -0.446,1 -1,1 -0.554,0 -1,-0.446 -1,-1 z m 4,0 h 2 v 2 c 0,0.554 -0.446,1 -1,1 -0.554,0 -1,-0.446 -1,-1 z"></path>',
        '<path d="M 7.5,15 C 7.223,15 7,15.223 7,15.5 V 16 H 6.5 C 6.223,16 6,16.223 6,16.5 6,16.777 6.223,17 6.5,17 H 7 v 0.5 C 7,17.777 7.223,18 7.5,18 7.777,18 8,17.777 8,17.5 V 17 h 2 v 0.5 c 0,0.277 0.223,0.5 0.5,0.5 0.277,0 0.5,-0.223 0.5,-0.5 V 17 h 2 v 0.5 c 0,0.277 0.223,0.5 0.5,0.5 0.277,0 0.5,-0.223 0.5,-0.5 V 17 h 2 v 0.5 c 0,0.277 0.223,0.5 0.5,0.5 0.277,0 0.5,-0.223 0.5,-0.5 V 17 h 0.5 C 17.777,17 18,16.777 18,16.5 18,16.223 17.777,16 17.5,16 H 17 V 15.5 C 17,15.223 16.777,15 16.5,15 16.223,15 16,15.223 16,15.5 V 16 H 14 V 15.5 C 14,15.223 13.777,15 13.5,15 13.223,15 13,15.223 13,15.5 V 16 H 11 V 15.5 C 11,15.223 10.777,15 10.5,15 10.223,15 10,15.223 10,15.5 V 16 H 8 V 15.5 C 8,15.223 7.777,15 7.5,15 Z"></path>',
        '<path d="m 12,14 c 2.33,0 4.31,1.46 5.11,3.5 H 6.89 C 7.69,15.46 9.67,14 12,14 Z"></path><path d="m 8,16 1,5 1,-5 z"></path><path d="m 14,16 1,5 1,-5 z"></path>'
    )
}


class GridySprite(SpriteBase):

    def sprite(self, random):
        body_bg1, body_bg2 = random.pickone(colors['bg'])
        eyes_fg1, eyes_fg2 = random.pickone(random.pickone(colors['fg']))
        mouth_fg1, mouth_fg2 = random.pickone(random.pickone(colors['fg']))
        body_path = random.pickone(paths['body'])
        eyes_path = random.pickone(paths['eyes'])
        mouth_path = random.pickone(paths['mouth'])
        svg_paths = [
            '<svg {svg_attr} viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">'
            '<defs><clipPath id="clip-a"><rect width="13" height="24" x="0" y="0"></rect></clipPath>',
            '<clipPath id="clip-b"><rect width="12" height="24" x="12" y="0"></rect></clipPath></defs>',
            '<g style="fill: {body_bg1}" clip-path="url(#clip-a)">{body_path}</g>',
            '<g style="fill: {body_bg2}" clip-path="url(#clip-b)">{body_path}</g>',
            '<g style="fill: {eyes_fg1}" clip-path="url(#clip-a)">{eyes_path}</g>',
            '<g style="fill: {eyes_fg2}" clip-path="url(#clip-b)">{eyes_path}</g>',
            '<g style="fill: {mouth_fg1}" clip-path="url(#clip-a)">{mouth_path}</g>',
            '<g style="fill: {mouth_fg2}" clip-path="url(#clip-b)">{mouth_path}</g>',
            '</svg>'
        ]
        svg = ''.join(svg_paths)

        return svg.format(
            svg_attr=self.svg_attr,
            body_bg1=body_bg1,
            body_bg2=body_bg2,
            eyes_fg1=eyes_fg1,
            eyes_fg2=eyes_fg2,
            mouth_fg1=mouth_fg1,
            mouth_fg2=mouth_fg2,
            body_path=body_path,
            eyes_path=eyes_path,
            mouth_path=mouth_path
        )
